
package com.bei_en.teacher.management.service.system;

import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bei_en.teacher.management.model.system.bean.SystemUserRole;
import com.bei_en.teacher.management.model.system.condition.SystemUserRoleCondition;
import com.bei_en.teacher.management.model.system.vo.SystemUserRoleVo;
import com.bei_en.teacher.management.persistence.dao.system.SystemUserRoleDao;
import com.bei_en.teacher.management.service.BaseService;

/**
 * 用户-角色
 * @author Longqs
 * @date 2016-6-15 15:05:57
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class SystemUserRoleService extends BaseService {

    @Autowired
    private SystemUserRoleDao systemUserRoleDao;

    public void insert(SystemUserRoleVo model) {

	systemUserRoleDao.insert(model.getSystemUserRole());
    }

//    /**
//     * 插入并返回生成的id
//     * @param model
//     * @return
//     */
//    public Integer merge(SystemUserRoleVo model) {
//
//	systemUserRoleDao.insert(model.getSystemUserRole());
//	return systemUserRoleDao.findLastInsertedId();
//
//    }

    public void delete(Integer modelInteger) {

	systemUserRoleDao.delete(modelInteger);
    }

    public SystemUserRoleVo findById(Integer modelInteger) {

	SystemUserRoleVo model = new SystemUserRoleVo(systemUserRoleDao.findById(modelInteger));
	return model;
    }

    public void update(SystemUserRoleVo model) {

	systemUserRoleDao.update(model.getSystemUserRole());
    }

    /**
     * 动态更新,只更新非null的值
     * @param model
     */
    public void updateSelective(SystemUserRoleVo model) {

	systemUserRoleDao.updateSelective(model.getSystemUserRole());
    }

    public int countAll() {

	return systemUserRoleDao.countAll();
    }

    public List<SystemUserRoleVo> findAll() {

	List<SystemUserRoleVo> list = new ArrayList<SystemUserRoleVo>();
	List<SystemUserRole> list1 = systemUserRoleDao.findAll();
	for (SystemUserRole model : list1) {
	    list.add(new SystemUserRoleVo(model));
	}
	return list;
    }

    public List<Integer> findAllIds() {

	return systemUserRoleDao.findAllIds();
    }

    /**
     * 分页查询，包括总数和内容
     * @param Vo  返回总数
     * @return  返回内容
     */
    public List<SystemUserRoleVo> findSystemUserRolePage(SystemUserRoleCondition vo) {

	Integer totalCount = searchSystemUserRoleNum(vo);
	List<SystemUserRoleVo> list = new ArrayList<SystemUserRoleVo>();
	if (totalCount>0) {
	    vo.setTotalCount(totalCount);
	    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "id");
	    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");

	    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
	    List<SystemUserRole> list1 = systemUserRoleDao.findPageBreakByCondition(vo, rb);
	    for (SystemUserRole model : list1) {
		list.add(new SystemUserRoleVo(model));
	    }

	}

	return list;
    }

    public String findSystemUserRolePageString(SystemUserRoleCondition vo) {

	Integer totalCount = searchSystemUserRoleNum(vo);
	String list = "";
	if (totalCount>0) {
	    vo.setTotalCount(totalCount);
	    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "id");
	    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");

	    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
	    List<SystemUserRole> list1 = systemUserRoleDao.findPageBreakByCondition(vo, rb);
	    for (SystemUserRole model : list1) {
		list += model.getRoleId()+",";
	    }
	    if (list.contains(","))
		list = list.substring(0, list.length()-1);

	}

	return list;
    }

    private Integer searchSystemUserRoleNum(SystemUserRoleCondition Vo) {

	return systemUserRoleDao.findNumberByCondition(Vo);
    }

}
